package com.lobo.blog.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lobo.blog.entity.Blog;
import com.lobo.blog.mappers.BlogMapper;
import com.lobo.blog.service.BlogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

import static com.lobo.blog.utils.MarkDownToHtmlTool.markdownToHtmlExtensions;

/**
 * @authoer:MacieSerenity
 * @createDate: 2022/10/4
 * @description:
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Resource
    BlogMapper blogMapper;

    private static final int pageSize = 10;

    @Override
    public Blog getBlogAndConvertoHtml(int blogId) {
        blogMapper.updateViews(blogId);
        Blog blogById = blogMapper.getBlogById(blogId);
        Blog b = new Blog(blogById);
        String HtmlContent = b.getContent();
        //将MarkDown语法的数据转为Html格式的数据
        b.setContent(markdownToHtmlExtensions(HtmlContent));
        return b;
    }

    @Override
    public Blog getBlogById(int blogId) {
        Blog blog = blogMapper.getBlogById(blogId);
        if (blog != null) {
            return blog;
        }
        return null;
    }

    @Override
    public PageInfo<Blog> getBlogs(int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogs = blogMapper.getAllBlogByList();
        PageInfo<Blog> blogPageInfo = new PageInfo(blogs);
        return blogPageInfo;
    }

    @Override
    public List<Blog> getRecentNewBlogBySize(int size) {
        return blogMapper.getRecentNewBlogBySize(size);
    }

    @Override
    public PageInfo<Blog> getBlogsByCondition(int pageNum, Blog blog) {
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogs = blogMapper.getAllBlogByCondition(blog);
        PageInfo<Blog> blogPageInfo = new PageInfo(blogs);
        return blogPageInfo;
    }

    @Override
    public PageInfo<Blog> getBlogsByTag(int pageNum, Integer tagId) {
        List<Integer> blogsIdList = blogMapper.getBlogIdsByTagId(tagId);
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogsByTagId = blogMapper.getBlogsByTagId(blogsIdList);
        PageInfo<Blog> pageInfo = new PageInfo(blogsByTagId);
        return pageInfo;
    }

    @Override
    public PageInfo<Blog> findBlogsByQuery(int pageNum, String query) {
        PageHelper.startPage(pageNum, pageSize);
        List<Blog> blogs = blogMapper.getBlogsByQuery(query);
        PageInfo<Blog> blogPageInfo = new PageInfo(blogs);
        return blogPageInfo;
    }
}
